/**
  ******************************************************************************
  * @file     SD93F115B_pwm.c
  * @author   TEST TEAM 												    	
  * @version  v0															    	
  * @date     Septemper 2022												    	
  * @brief
  *
  * Copyright (C) 2022  Hangzhou SDIC Microelectronics Co., Ltd
**********************************************************************************/
    
/* Includes*/    
#include "SD93F115B_pwm.h"
#include "SD93F115B_rcc.h"
 
/*------------------------------------------
 * 函数功能：PWM_PDM使能
 * 入口参数：PWMx: where x can be (0,1) to select the PWM peripheral.
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *------------------------------------------*/
void PWM_PDM_DeInit(PWM_TypeDef * PWMx) 
 {
	/* Check the parameters */
	assert_param(IS_PWM_ALL_PERIPH(PWMx)); 
	if(PWMx==PWM0)
	{
		RCC_APBPeriphResetCmd(RCC_APBPeriph_PWM0, ENABLE);
	    RCC_APBPeriphResetCmd(RCC_APBPeriph_PWM0, DISABLE); 
    }
	else 
		if(PWMx==PWM1)
		{
			RCC_APBPeriphResetCmd(RCC_APBPeriph_PWM1, ENABLE);
            RCC_APBPeriphResetCmd(RCC_APBPeriph_PWM1, DISABLE); 
		}
 }
 
 /*----------------------------------------------------------------------
 * 函数功能：初始化PWM功能
 * 入口参数：PWMx,PWM_InitStruct
 *           PWMx: where x can be (0,1) to select the PWM peripheral.
 * 			 PWM_InitStruct: pointer to a PWM_InitStruct structure that contains
 *           the configuration information for the specified PWM peripheral.
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *-----------------------------------------------------------------------*/
void PWM_PDM_Init(PWM_TypeDef * PWMx,PWM_InitTypeDef* PWM_InitStruct) 
{
	/* Check the parameters */
	if(PWM_InitStruct->PWM_PDM_Mode==PWM_Mode)
	{
		PWM_PDM_ModeConfig(PWMx,PWM_InitStruct->PWM_PDM_Mode);	
        PWM_WaveModeCmd(PWMx,PWM_InitStruct->PWM_WaveMode);
	    PWM_SetDutyCycle(PWMx,PWM_InitStruct->PWM_DutyCycle);
	    PWM_SetPeriod(PWMx,PWM_InitStruct->PWM_Period);
	
	}
	else
	{
		PWM_PDM_ModeConfig(PWMx,PWM_InitStruct->PWM_PDM_Mode);
		PDM_SetInputData(PWMx,PWM_InitStruct->PDM_DATA);
		
	}

}

/*--------------------------------------------------------------
 * 函数功能：PWM_PDM使能
 * 入口参数：PWMx,NewState
 *			 PWMx: where x can be (0,1) to select the PWM peripheral.
 * 			 NewState: ENABLE或者DISABLE
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *--------------------------------------------------------------*/
void PWM_PDM_Cmd(PWM_TypeDef * PWMx ,FunctionalState NewState)
{
	/* Check the parameters */
	assert_param(IS_PWM_ALL_PERIPH(PWMx));
	assert_param(IS_FUNCTIONAL_STATE(NewState));
	if (NewState != DISABLE)
	{
		PWMx->CR |= PWM_CR_PMEN;
	}
	else
	{
		PWMx->CR &=~ PWM_CR_PMEN;
	}		 
}

 /*----------------------------------------------------------------
 * 函数功能：选择以PWM方式输出，还是以PDM方式输出 
 * 入口参数：PWMx,PWM_PDM_Mode
 * 			 PWMx: where x can be (0,1) to select the PWM peripheral.
 *           PWM_PDM_Mode: PWM_Mode,PDM_Mode
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *-----------------------------------------------------------------*/
void PWM_PDM_ModeConfig(PWM_TypeDef * PWMx,uint8_t PWM_PDM_Mode)                               
 {
	/* Check the parameters */
	assert_param(IS_PWM_ALL_PERIPH(PWMx));
	assert_param(IS_PWM_PDM_Mode(PWM_PDM_Mode));
	
    if (PWM_PDM_Mode != PWM_Mode)
	{
		PWMx->CR |= PWM_CR_PMSEL;
	}
	else
	{
		PWMx->CR &= ~PWM_CR_PMSEL;
	} 
	
 }
 /*------------------------------------------------------------------
 * 函数功能：选择PWM波形输出模式 
 * 入口参数：PWMx,NewState
 * 			 PWMx: where x can be (0,1) to select the PWM peripheral.
 * 			 NewState: ENABLE或者DISABLE
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *-------------------------------------------------------------------*/
void PWM_WaveModeCmd(PWM_TypeDef * PWMx,FunctionalState NewState)
{
	/* Check the parameters */
	assert_param(IS_PWM_ALL_PERIPH(PWMx));
	assert_param(IS_FUNCTIONAL_STATE(NewState));
	
	if (NewState != DISABLE)
	{
		PWMx->CR |= PWM_CR_PWMS;
	}
	else
	{
		PWMx->CR &=~ PWM_CR_PWMS;
	} 
}                 

 /*---------------------------------------------------------------
 * 函数功能：PWM 占空比设置
 * 入口参数：PWMx,PWM_DutyCycle
 * 			 PWMx: where x can be (0,1) to select the PWM peripheral.
 *           PWM_DutyCycle: 占空比可设置为0x0000~0xffff，但不能超过周期设置
 *           PWM_DutyCycle: 
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *----------------------------------------------------------------*/
void PWM_SetDutyCycle(PWM_TypeDef * PWMx,uint16_t PWM_DutyCycle)
{
	/* Check the parameters */
	assert_param(IS_PWM_ALL_PERIPH(PWMx));	
	assert_param(IS_PWM_DutyCycle(PWM_DutyCycle)); 
	
    PWMx->DR = PWM_DutyCycle;	 
}

/*-----------------------------------------------------------
 * 函数功能：PWM 周期设置
 * 入口参数：PWMx,PWM_Period
 * 			 PWMx: where x can be (0,1) to select the PWM peripheral.
 *           PWM_Period: 周期可设置为0x0000~0xffff
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *----------------------------------------------------------*/
void PWM_SetPeriod(PWM_TypeDef * PWMx,uint16_t PWM_Period)
{
	/* Check the parameters */
	assert_param(IS_PWM_ALL_PERIPH(PWMx));
	assert_param(IS_PWM_Period(PWM_Period));
	
    PWMx->TR = PWM_Period;
}

/*---------------------------------------------------------------
 * 函数功能：设置PDM输入数据，以调整PDM的脉冲
 * 入口参数：PWMx,PDM_DATA
 * 			 PWMx: where x can be (0,1) to select the PWM peripheral.
 *           PDM_DATA: PDM输入数据可设置为0x0000~0xffff
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *---------------------------------------------------------------*/
void PDM_SetInputData(PWM_TypeDef * PWMx,uint16_t PDM_DATA)
{
	/* Check the parameters */
	assert_param(IS_PWM_ALL_PERIPH(PWMx));
	assert_param(IS_PWM_DutyCycle(PDM_DATA));
    PWMx->DR = PDM_DATA;

}

/*-------------------------------------------------------------
 * 函数功能：使能PWM周期溢出中断功能
 * 入口参数：PWMx,NewState
 * 			 PWMx: where x can be (0,1) to select the PWM peripheral.
 * 			 NewState: ENABLE或者DISABLE
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *------------------------------------------------------------*/
void PWM_ITCmd(PWM_TypeDef * PWMx ,FunctionalState NewState)
{
	/* Check the parameters */
	assert_param(IS_PWM_ALL_PERIPH(PWMx));
	assert_param(IS_FUNCTIONAL_STATE(NewState));
	
	if (NewState != DISABLE)
	{
		PWMx->CR |= PWM_CR_PWMIE;
	}
	else
	{
		PWMx->CR &= ~PWM_CR_PWMIE;
	}	
}

/*------------------------------------------------
 * 函数功能：读PWM中断状态
 * 入口参数：PWMx: where x can be (0,1) to select the PWM peripheral.
 * 出口参数：None
 * 返回值  ：bitstatus
 * 编写日期：
 * 作者    ：
 *------------------------------------------------*/
ITStatus PWM_GetITStatus(PWM_TypeDef * PWMx)
{
	/* Check the parameters */
	assert_param(IS_PWM_ALL_PERIPH(PWMx));
	
	ITStatus bitstatus = RESET;
	
	if ((PWMx->SR & PWM_SR_PWMIF) != RESET)
	{
		bitstatus = SET;
	}
	else
	{
		bitstatus = RESET;
	}
	return bitstatus;		
}
/*---------------------------------------------
 * 函数功能：读PWM中断状态
 * 入口参数：PWMx: where x can be (0,1) to select the PWM peripheral.
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *---------------------------------------------*/
void PWM_ClearITStatus(PWM_TypeDef * PWMx)
{	
    /* Check the parameters */
	assert_param(IS_PWM_ALL_PERIPH(PWMx));
	
	PWMx->SR|=PWM_SR_PWMIF;
	
}


/****************************END OF FILE****************************/

